package com.issue.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.issue.common.core.domain.entity.SysUser;
import com.issue.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.issue.common.annotation.Log;
import com.issue.common.core.controller.BaseController;
import com.issue.common.core.domain.AjaxResult;
import com.issue.common.enums.BusinessType;
import com.issue.system.domain.AChildInfo;
import com.issue.system.service.IAChildInfoService;
import com.issue.common.utils.poi.ExcelUtil;
import com.issue.common.core.page.TableDataInfo;

/**
 * Child InfoController
 * 
 * @author system
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/system/child_info")
public class AChildInfoController extends BaseController
{
    @Autowired
    private IAChildInfoService aChildInfoService;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询Child Info列表
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:list')")
    @GetMapping("/list")
    public TableDataInfo list(AChildInfo aChildInfo)
    {
        startPage();
        List<AChildInfo> list = aChildInfoService.selectAChildInfoList(aChildInfo);
        return getDataTable(list);
    }

    /**
     * 查询Child Info列表
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(AChildInfo aChildInfo)
    {
        List<AChildInfo> list = aChildInfoService.selectAChildInfoList(aChildInfo);
        return getDataTable(list);
    }


    /**
     * 导出Child Info列表
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:export')")
    @Log(title = "Child Info", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AChildInfo aChildInfo)
    {
        List<AChildInfo> list = aChildInfoService.selectAChildInfoList(aChildInfo);
        ExcelUtil<AChildInfo> util = new ExcelUtil<AChildInfo>(AChildInfo.class);
        util.exportExcel(response, list, "Child Info数据");
    }

    /**
     * 获取Child Info详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:query')")
    @GetMapping(value = "/{childId}")
    public AjaxResult getInfo(@PathVariable("childId") Long childId)
    {
        return success(aChildInfoService.selectAChildInfoByChildId(childId));
    }

    /**
     * 新增Child Info
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:add')")
    @Log(title = "Child Info", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AChildInfo aChildInfo)
    {
        // 根据手机号查询老师
        SysUser sysUser = userMapper.checkPhoneUnique(aChildInfo.getGuardianPhone());
        if (sysUser == null){
            return AjaxResult.error("The guardian cannot be reached by phone number");
        }
        aChildInfo.setGuardianId(sysUser.getUserId());
        aChildInfo.setGuardianName(sysUser.getUserName());
        aChildInfo.setGuardianPhone(sysUser.getPhonenumber());

        sysUser = userMapper.checkPhoneUnique(aChildInfo.getTeacherPhone());
        if (sysUser == null){
            return AjaxResult.error("The Teacher cannot be reached by phone number");
        }
        aChildInfo.setTeacherId(sysUser.getUserId());
        aChildInfo.setTeacherName(sysUser.getUserName());
        aChildInfo.setTeacherPhone(sysUser.getPhonenumber());

        return toAjax(aChildInfoService.insertAChildInfo(aChildInfo));
    }

    /**
     * 修改Child Info
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:edit')")
    @Log(title = "Child Info", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AChildInfo aChildInfo)
    {
        // 根据手机号查询老师
        SysUser sysUser = userMapper.checkPhoneUnique(aChildInfo.getGuardianPhone());
        if (sysUser == null){
            return AjaxResult.error("The guardian cannot be reached by phone number");
        }
        aChildInfo.setGuardianId(sysUser.getUserId());
        aChildInfo.setGuardianName(sysUser.getUserName());
        aChildInfo.setGuardianPhone(sysUser.getPhonenumber());

        sysUser = userMapper.checkPhoneUnique(aChildInfo.getTeacherPhone());
        if (sysUser == null){
            return AjaxResult.error("The Teacher cannot be reached by phone number");
        }
        aChildInfo.setTeacherId(sysUser.getUserId());
        aChildInfo.setTeacherName(sysUser.getUserName());
        aChildInfo.setTeacherPhone(sysUser.getPhonenumber());

        return toAjax(aChildInfoService.updateAChildInfo(aChildInfo));
    }

    /**
     * 删除Child Info
     */
    @PreAuthorize("@ss.hasPermi('system:child_info:remove')")
    @Log(title = "Child Info", businessType = BusinessType.DELETE)
	@DeleteMapping("/{childIds}")
    public AjaxResult remove(@PathVariable Long[] childIds)
    {
        return toAjax(aChildInfoService.deleteAChildInfoByChildIds(childIds));
    }
}
